<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>哔哩哔哩弹幕视频网</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script type="importmap">
    {
      "imports": {
        "vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js"
      }
    }
    </script>
    <style>
        .hero-section {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            padding: 4rem 0;
            margin-bottom: 2rem;
        }
        .video-card {
            transition: transform 0.3s ease;
            margin-bottom: 1.5rem;
        }
        .video-card:hover {
            transform: translateY(-5px);
        }
        .video-thumbnail {
            position: relative;
            overflow: hidden;
            border-radius: 8px;
        }
        .video-thumbnail img {
            width: 100%;
            height: 200px;
            object-fit: cover;
        }
        .video-duration {
            position: absolute;
            bottom: 10px;
            right: 10px;
            background: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 0.8rem;
        }
        .video-info {
            padding: 1rem;
        }
        .video-title {
            font-weight: bold;
            margin-bottom: 0.5rem;
        }
        .video-meta {
            color: #666;
            font-size: 0.9rem;
        }
        .search-box {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 25px;
            padding: 0.5rem 1rem;
            margin-top: 1rem;
        }
        .search-box input {
            background: transparent;
            border: none;
            color: white;
            width: 100%;
            padding: 0.5rem;
        }
        .search-box input::placeholder {
            color: rgba(255, 255, 255, 0.7);
        }
        .search-box input:focus {
            outline: none;
        }
    </style>
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <div class="container">
                <a class="navbar-brand" href="/">
                    <i class="fas fa-play-circle"></i> 哔哩哔哩弹幕网
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav me-auto">
                        <li class="nav-item">
                            <a class="nav-link active" href="/"><i class="fas fa-home"></i> 首页</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/upload.html" @click="handleUploadClick"><i class="fas fa-upload"></i> 投稿</a>
                        </li>
                    </ul>
                    <ul class="navbar-nav">
                        <li class="nav-item" id="loginBtn" v-show="!isAuthenticated">
                            <a class="nav-link" href="/login.html"><i class="fas fa-sign-in-alt"></i> 登录</a>
                        </li>
                        <li class="nav-item" id="registerBtn" v-show="!isAuthenticated">
                            <a class="nav-link" href="/register.html"><i class="fas fa-user-plus"></i> 注册</a>
                        </li>
                        <li class="nav-item" id="profileBtn" v-show="isAuthenticated">
                            <a class="nav-link" href="/profile.html" @click="handleProfileClick"><i class="fas fa-user"></i> {{ currentUser?.username }}</a>
                        </li>
                        <li class="nav-item" id="uploadBtn" v-show="isAuthenticated">
                            <a class="nav-link" href="/upload.html"><i class="fas fa-upload"></i> 投稿</a>
                        </li>
                        <li class="nav-item" id="logoutBtn" v-show="isAuthenticated">
                            <a class="nav-link" href="#" @click="handleLogout"><i class="fas fa-sign-out-alt"></i> 退出</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>

        <section class="hero-section">
            <div class="container text-center">
                <h1 class="display-4">欢迎来到哔哩哔哩弹幕视频网</h1>
                <p class="lead">在这里分享、观看、互动弹幕视频</p>
                <div class="mt-4">
                    <div id="authButtons" v-show="!isAuthenticated">
                        <a href="/login.html" class="btn btn-light btn-lg me-3">
                            <i class="fas fa-sign-in-alt"></i> 登录
                        </a>
                        <a href="/register.html" class="btn btn-outline-light btn-lg">
                            <i class="fas fa-user-plus"></i> 立即注册
                        </a>
                    </div>
                </div>
                <div class="search-box mt-4">
                    <input type="text" v-model="searchTerm" @input="searchVideos" placeholder="搜索视频、UP主、番剧...">
                </div>
            </div>
        </section>

        <div class="container mt-4">
            <div class="row mb-4">
                <div class="col-12">
                    <div class="btn-group" role="group">
                        <button type="button" 
                                class="btn btn-outline-primary" 
                                :class="{ active: currentFilter === 'all' }"
                                @click="filterVideos('all')">全部视频</button>
                        <button type="button" 
                                class="btn btn-outline-primary" 
                                :class="{ active: currentFilter === 'trending' }"
                                @click="filterVideos('trending')">热门</button>
                        <button type="button" 
                                class="btn btn-outline-primary" 
                                :class="{ active: currentFilter === 'newest' }"
                                @click="filterVideos('newest')">最新</button>
                    </div>
                </div>
            </div>
            
            <div class="row" id="videoList">
                <div v-if="loading" class="col-12 text-center">
                    <div class="spinner-border text-primary" role="status">
                        <span class="visually-hidden">Loading...</span>
                    </div>
                </div>
                <div v-else-if="filteredVideos.length === 0" class="col-12 text-center">
                    <h3>No videos found</h3>
                </div>
                <div v-else v-for="video in filteredVideos" :key="video.id" class="col-md-4 col-sm-6 mb-4">
                    <div class="video-card">
                        <a :href="`/player.html?id=${video.id}`" class="text-decoration-none">
                            <div class="video-thumbnail">
                                <img :src="`/api/videos/${video.id}/thumbnail`" 
                                     :alt="video.title"
                                     @error="handleImageError">
                            </div>
                            <div class="video-info">
                                <h5 class="video-title text-dark">{{ video.title }}</h5>
                                <p class="video-description text-muted">{{ video.description || '' }}</p>
                                <div class="video-meta">
                                    <span class="me-2">
                                        <i class="fas fa-calendar"></i> {{ formatDate(video.createdAt) }}
                                    </span>
                                </div>
                            </div>
                        </a>
                        <div v-if="isOwner(video)" class="video-actions mt-2">
                            <button class="btn btn-sm btn-danger" @click="deleteVideo(video.id)">
                                <i class="fas fa-trash"></i> Delete
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <footer class="bg-dark text-light mt-5 py-4">
            <div class="container">
                <div class="row">
                    <div class="col-md-6">
                        <h5>关于哔哩哔哩弹幕网</h5>
                        <p>一个可以投稿、观看、互动弹幕的二次元社区，快来加入我们吧！</p>
                    </div>
                    <div class="col-md-3">
                        <h5>快速链接</h5>
                        <ul class="list-unstyled">
                            <li><a href="/" class="text-light">首页</a></li>
                            <li><a href="/upload.html" class="text-light">投稿</a></li>
                            <li><a href="#" class="text-light">用户协议</a></li>
                            <li><a href="#" class="text-light">隐私政策</a></li>
                        </ul>
                    </div>
                    <div class="col-md-3">
                        <h5>关注我们</h5>
                        <div class="social-links">
                            <a href="#" class="text-light me-2"><i class="fab fa-twitter"></i></a>
                            <a href="#" class="text-light me-2"><i class="fab fa-facebook"></i></a>
                            <a href="#" class="text-light me-2"><i class="fab fa-instagram"></i></a>
                            <a href="#" class="text-light"><i class="fab fa-github"></i></a>
                        </div>
                    </div>
                </div>
                <div class="row mt-3">
                    <div class="col-12 text-center">
                        <small>&copy; 2025 哔哩哔哩弹幕视频网. 保留所有权利。</small>
                    </div>
                </div>
            </div>
        </footer>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script type="module">
        import { createApp, ref, computed, onMounted, watch } from 'vue';
        import { authState, checkAuthStatus, logout, showToast } from '/js/auth.js';

        createApp({
            setup() {
                const videos = ref([]);
                const loading = ref(true);
                const currentFilter = ref('all');
                const searchTerm = ref('');

                // Auth computed properties
                const isAuthenticated = computed(() => authState.isAuthenticated);
                const currentUser = computed(() => authState.currentUser);

                // Computed properties
                const filteredVideos = computed(() => {
                    let filtered = videos.value;
                    
                    // Apply search filter
                    if (searchTerm.value) {
                        const term = searchTerm.value.toLowerCase();
                        filtered = filtered.filter(video => 
                            video.title.toLowerCase().includes(term) || 
                            (video.description && video.description.toLowerCase().includes(term))
                        );
                    }
                    
                    // Apply category filter
                    switch (currentFilter.value) {
                        case 'trending':
                            // For now, just return all videos. You can implement trending logic here
                            return filtered;
                        case 'newest':
                            return filtered.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
                        default:
                            return filtered;
                    }
                });

                // Methods
                const loadVideos = async () => {
                    try {
                        loading.value = true;
                        const response = await axios.get('/api/videos/public');
                        videos.value = response.data;
                    } catch (error) {
                        console.error('Error loading videos:', error);
                        showToast('Error loading videos', 'error');
                    } finally {
                        loading.value = false;
                    }
                };

                const filterVideos = (filter) => {
                    currentFilter.value = filter;
                };

                const searchVideos = () => {
                    // Search is handled by computed property
                };

                const isOwner = (video) => {
                    return currentUser.value && video.userId === currentUser.value.id;
                };

                const deleteVideo = async (videoId) => {
                    if (!confirm('Are you sure you want to delete this video?')) {
                        return;
                    }

                    try {
                        await axios.delete(`/api/videos/${videoId}`);
                        showToast('Video deleted successfully', 'success');
                        loadVideos(); // Reload the video list
                    } catch (error) {
                        console.error('Error deleting video:', error);
                        showToast('Error deleting video', 'error');
                    }
                };

                const handleUploadClick = async (event) => {
                    event.preventDefault();
                    const isLoggedIn = await checkAuthStatus(true);
                    if (!isLoggedIn) {
                        window.location.href = '/login.html?redirect=/upload.html';
                    } else {
                        window.location.href = '/upload.html';
                    }
                };

                const handleProfileClick = async (event) => {
                    event.preventDefault();
                    const isLoggedIn = await checkAuthStatus(true);
                    if (!isLoggedIn) {
                        window.location.href = '/login.html?redirect=/profile.html';
                    } else {
                        window.location.href = '/profile.html';
                    }
                };

                const handleLogout = async (event) => {
                    event.preventDefault();
                    await logout();
                };

                const formatDate = (dateString) => {
                    return new Date(dateString).toLocaleDateString();
                };

                const handleImageError = (event) => {
                    event.target.src = '';
                };

                // Lifecycle
                onMounted(async () => {
                    // 检查用户是否已登录，但不强制验证（允许匿名访问）
                    await checkAuthStatus(false);
                    await loadVideos();
                });

                // Watch for auth changes and reload videos
                watch(isAuthenticated, () => {
                    loadVideos();
                });

                return {
                    videos,
                    loading,
                    currentFilter,
                    searchTerm,
                    isAuthenticated,
                    currentUser,
                    filteredVideos,
                    loadVideos,
                    filterVideos,
                    searchVideos,
                    isOwner,
                    deleteVideo,
                    handleUploadClick,
                    handleProfileClick,
                    handleLogout,
                    formatDate,
                    handleImageError
                };
            }
        }).mount('#app');
    </script>
</body>
</html>
